import React from 'react';
import {combineReducers, createStore} from 'redux'
// import reducers from '../router'
import {persistReducer, persistStore} from 'redux-persist';
import localStorage from 'redux-persist/lib/storage';
import storageSession from 'redux-persist/lib/storage/session'
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';

import News from "../components/pages/News";
import Case from "../components/pages/Case";
import Business from "../components/pages/Business";
import Contact_Us from "../components/pages/ContactUs";
import MainContent from "../components/pages/MainContent";
import Message from "../components/pages/Message";
import About from "../components/pages/About";
import BusinessDetail from "../components/pages/BusinessDetail";


export const mumesData = [
    {t_name: '首页', t_path: '/', name: 'home', component: MainContent,level:1},
    {t_name: '关于我们', t_path: '/about', name: 'about', component: About,level:1},
    {t_name: '业务领域', t_path: '/business', name: 'business', component: Business ,level:1},
    // {t_name: '服务流程', t_path: '/process', name: 'process', component: Business,level:1},
    {t_name: '服务案例', t_path: '/case', name: 'case', component: Case,level:1},
    {t_name: '新闻中心', t_path: '/news', name: 'news', component: News,level:1},
    {t_name: '在线留言', t_path: '/message', name: 'message', component: Message,level:1},
    {t_name: '联系我们', t_path: '/contactUs', name: 'contactUs', component: Contact_Us,level:1},

    {t_name: '业务详情', t_path: '/businessDetail', name: 'businessDetail', component: BusinessDetail,level:2}
]


const __memus = function (state, action) {
    state = mumesData
    if (action.name != 'memus') {
        return state
    }
    action = action || {
        type: -1
    }
    const {type} = action
    const result = type >= 0 ? state[type] : state
    return result
}
// const memus = createStore(__memus)
// memus.subscribe(() => {
//     console.log("memus update")
// })
const memusPersistConfig = {
    key: 'memus',
    storage: localStorage,
    stateReconciler: autoMergeLevel2
}


const __memu_index = function (state, action) {
    state = "0"
    if (action.name != 'memu_index') {
        ///nothing
    }else{
        const {type} = action
        if (type >= 0 && type <= mumesData.length) {
            state = type+"";
        }
    }
    return state
}
// const memu_index = createStore(__memu_index)
// memu_index.subscribe(() => {
//     console.log("user_info update")
// })

const memu_indexPersistConfig = {
    key: 'memu_index',
    storage: localStorage,
    stateReconciler: autoMergeLevel2
}


const __user_info = function (state, action) {
    state = state || {
        userNaem: 'admin',
        phone: '12298998766'
    }
    if (action.name != 'user_info') {
        return state
    }
    switch (action.type) {
        case 'ADMIN':
            return {
                userNaem: 'admin1',
                phone: '12298998766'
            }
        case 1:
            return {
                userNaem: 'admin2',
                phone: '12298998766'
            }
        default:
            return state
    }
}
// const user_info = createStore(__user_info)
// user_info.subscribe(() => {
//     console.log("user_info update")
// })
const user_infoPersistConfig = {
    key: 'user_info',
    storage: storageSession,
    stateReconciler: autoMergeLevel2
}

// const rootReducer = combineReducers({
//     'memus': persistReducer(memusPersistConfig, __memus),
//     'memu_index': persistReducer(memu_indexPersistConfig, __memu_index),
//     'user_info': persistReducer(user_infoPersistConfig, __user_info),
// })

const rootReducer = combineReducers({
    'memus': __memus,
    'memu_index': __memu_index,
    'user_info': __user_info,
})

// const rootReducer = function combinedReducer(state, action) {
//     const df = {
//         'memus': persistReducer(memusPersistConfig, __memus(state, action)),
//         'memu_index': persistReducer(memu_indexPersistConfig, __memu_index(state, action)),
//         'user_info': persistReducer(user_infoPersistConfig, __user_info(state, action))
//     }
//     return df;
// }
const rootPersistConfig = {
    key: 'root-key',
    storage: localStorage,
    stateReconciler: autoMergeLevel2 // 查看 'Merge Process' 部分的具体情况
};

const rootPersistReducer = persistReducer(rootPersistConfig, rootReducer)

export const store = createStore(rootPersistReducer)
export const persistorStore = persistStore(store)
export const to = (type = 0) => {
    const memus = store.getState('memus')
    if (memus instanceof Array) {
        type = memus.findIndex(x => x.t_path === name || x.name === name)
    }
    store.dispatch({
        name: this.name,
        type: type
    })
}
export const memus = {
    name: "memus",
    getState() {
        return store.getState()[this.name];
        // const result = []
        // const tmp =store.getState()[this.name];
        // if(tmp){
        //     for(let key in tmp){
        //         result.push(tmp[key])
        //     }
        //     result.pop();
        // }
        // return result
    },
    dispatch(type){
        const name = this.name
        return store.dispatch({temp, type})
    }
}
export const memu_index = {
    name: "memu_index",
    getState() {
        return store.getState()[this.name];
        // const result = []
        // console.log(store.getState()[this.name])
        // const tmp =store.getState()[this.name];
        // if(tmp){
        //     for(let key in tmp){
        //         result.push(tmp[key])
        //     }
        //     if(result.length==0){
        //         return -1;
        //     }
        //     result.pop();
        // }
        // return result[0]
    },
    dispatch(type){
        const name = this.name
        return store.dispatch({name, type})
    }
}
export const user_info = {
    name: "user_info",
    getState() {
        return store.getState()[this.name]
    },
    dispatch(type){
        const name = this.name
        return store.dispatch({temp, type})
    }
}
// export const memus = store.getState("memus")
// export const memu_index = store.getState("memu_index")
// export const user_info = store.getState("user_info")

const $redux = {store, persistorStore, to, memus, memu_index, user_info}
$redux.store.subscribe((e) => {
    console.log("store update ...")
})


React.Component.prototype.$redux = $redux
export default $redux
